//
//  HomeBannerCell.swift
//  d11
//
//  Created by peale on 2018/8/7.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class HomeBannerCell: UITableViewCell,FSPagerViewDataSource,FSPagerViewDelegate {
    var pagerView : FSPagerView?
    var imageUrls : [String]?
    var pageControl : FSPageControl?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.white
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.imageUrls = []
        self.pagerView = FSPagerView.init(frame: self.bounds)
        self.pagerView?.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        //self.pagerView?.itemSize = .zero
        self.pagerView?.interitemSpacing = 10 //间距
        self.pagerView?.automaticSlidingInterval = 5 //间隔时间
        self.pagerView?.isInfinite = true //循环滚动
         self.pagerView?.dataSource = self
        self.pagerView?.delegate = self
        self.pagerView?.transformer = FSPagerViewTransformer(type:.cubic)
       // let transform = CGAffineTransform(scaleX: 0.99, y: 0.99)
       // self.pagerView?.itemSize = (self.pagerView?.frame.size.applying(transform))!
         self.contentView.addSubview(pagerView!)
        self.pagerView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.centerX.equalTo(self.contentView)
        }
        
        self.pageControl = FSPageControl.init(frame: self.bounds)
        self.pageControl?.numberOfPages = self.imageUrls!.count
        self.pageControl?.contentHorizontalAlignment = .right
        self.pageControl?.contentInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        self.pageControl?.hidesForSinglePage = true
        self.pageControl?.contentHorizontalAlignment = .center
        self.contentView.addSubview(pageControl!)
        self.pageControl?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.bottom.equalToSuperview()
        }
    }
    
    func setData(imgUrls:[String]) -> Void {
        self.imageUrls = imgUrls
        self.pageControl?.numberOfPages = self.imageUrls!.count
        self.pagerView?.reloadData()
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        cell.imageView?.sd_setImage(with: URL.init(string: self.imageUrls![index]), completed: { (img, error, type, url) in
            
        })
        cell.imageView?.contentMode = .scaleToFill
        cell.imageView?.clipsToBounds = true
  //      cell.textLabel?.text = index.description+index.description
        return cell
    }
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.imageUrls!.count
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
        self.pageControl?.currentPage = index
    }
    
    func pagerViewDidScroll(_ pagerView: FSPagerView) {
        guard self.pageControl?.currentPage != pagerView.currentIndex else {
            return
        }
        self.pageControl?.currentPage = pagerView.currentIndex // Or Use KVO with property "currentIndex"
    }
}
